﻿// ActionScript file
/**
 * @usage 滚动框
 * 
 * @局限
 * UI视图中scrollbar名字必须为sb,滚动按钮name为thumb,轨迹name为track;
 * thumb&track.y/x必须为0;content.y/x必须为0;
 * 
 * @experience
 * 当scrollContentHeight不是绝对赋值时，即通过[content].height-[mask].height动态取得时，需注意以下几点：
 * 1、包含静态文本的MovieClip的实际宽高与显示宽高会发生偏差，
 * 	如实际高度为499，[mc].height则为561.35,此会造成滚动距离偏差.
 * 2、遮罩被缩放后(scaleX/scaleY)发生改变时，会取不到显示宽高.
 */
package org.nickro.controls{
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.nickro.events.ScrollEvent;
	import org.nickro.externallibrary.tween.TweenLite;
	import org.nickro.externallibrary.tween.easing.*;
	public class ScrollBox extends MovieClip
	{
		public static const MOVE:String = "move";
		
		private var yMax:Number;
		private var yMin:Number;
		private var yOffset:Number;
		private var _scrollContentFudge:uint;
		
		public function ScrollBox()
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void
		{
			addEventListener(Event.ENTER_FRAME, delayCall); 
		}
		
		private function delayCall(e:Event):void
		{
			removeEventListener(Event.ENTER_FRAME, delayCall);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
			
			yMax = (sb.track.height - sb.thumb.height);
			yMin = 0;
			initScroll();
		}
		
		private function onRemoved(e:Event):void
		{
			destroyScroll();
		}
		public function initScroll():void
		{
			sb.thumb.addEventListener(MouseEvent.MOUSE_DOWN,onThumbDown);
			sb.thumb.buttonMode = true;
			if(_stage == null)
			{
				throw new Error("Stage isn't unExisted");
			}else
			{
				_stage.addEventListener(MouseEvent.MOUSE_UP,onThumbUp);
			}
		}
		
		private function onThumbDown(e:MouseEvent):void
		{
			_stage.addEventListener(MouseEvent.MOUSE_MOVE,onThumbMove);
			yOffset = (mouseY - sb.thumb.y);
		}
		
		private function onThumbMove(e:MouseEvent):void
		{
			var percent:Number;
			sb.thumb.y = mouseY - yOffset;
			if(sb.thumb.y <= yMin)
			{
				sb.thumb.y = yMin;
			}
			if(sb.thumb.y >= yMax)
			{
				sb.thumb.y = yMax;
			}
			percent = (sb.thumb.y / yMax);
			TweenLite.to(_scrollContent, 1, { y: -((percent * _scrollContentHeight)), onUpdate:sendRenderEvent } );
			e.updateAfterEvent();
		}
		
		private function sendRenderEvent():void
		{
			dispatchEvent(new ScrollEvent(ScrollEvent.MOVE,_scrollContent.y));
		}
		
		private function onThumbUp(e:MouseEvent):void
		{
			_stage.removeEventListener(MouseEvent.MOUSE_MOVE,onThumbMove);
		}
		
		public function destroyScroll():void
		{
			trace("ScrollBox: destroyScroll()");
            sb.thumb.removeEventListener(MouseEvent.MOUSE_DOWN, onThumbDown);
            _stage.removeEventListener(MouseEvent.MOUSE_UP, onThumbUp);
            _stage.removeEventListener(MouseEvent.MOUSE_MOVE, onThumbMove);
		}
		
		/*private function positionThumb(pos:Number):void{
            var percent:Number;
            percent = (pos / ((_scrollContentHeight - _scrollBoxHeight) + _scrollContentFudge));
            sb.thumb.y = (percent * yMax);
            if (sb.thumb.y >= yMax)
            {
                sb.thumb.y = yMax;
            };
            if (sb.thumb.y <= yMin)
            {
                sb.thumb.y = yMin;
            };
        }*/
		

		//stage实例
		private var _stage:Stage;
		public function set stageInstance(value:Stage):void{_stage = value;}
		//滚动bar的高度
		private var _scrollBoxHeight:uint = 0;
		public function set scrollBoxHeight(value:Number):void { _scrollBoxHeight = value; };
		public function get scrollBoxHeight():Number { return _scrollBoxHeight; };
		/**
		 * @param scrollContent	滚动目标
		 */
		private var _scrollContent:*;
		public function set scrollContent(value:*):void { _scrollContent = value; };
		public function get scrollContent():* { return _scrollContent; };
		/**
		 * @param	scrollContentHeight	目标滚动的高度,滚动的距离(content.height-mask.height)
		 */
		private var _scrollContentHeight:Number;
		public function set scrollContentHeight(value:Number):void { _scrollContentHeight = value; };
		public function get scrollContentHeight():Number { return _scrollContentHeight; };
		/**
		 * @param	direction	滚动的方向
		 */
		private var _direction:String;
		public function set direction(value:String):void { _direction = value; };
		public function get derection():String { return _direction; };
	}
}